package org.example.music_manage_system.service.lmpl;

import org.example.music_manage_system.mapper.CategoryMapper;
import org.example.music_manage_system.pojo.Category;
import org.example.music_manage_system.service.CategoryService;
import org.example.music_manage_system.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void add(Category category) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");//获取当前用户id值
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(id);//将当前用户id值赋给创建分类用户Create_User字段
        categoryMapper.add(category);
    }
    //显示所有分类
    @Override
    public List<Category> List() {
//        获取用户id
        Map<String,Object> map=ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return  categoryMapper.List(userId);

    }
    //查找分类
    @Override
    public Category findById(Integer id) {
        Category category = categoryMapper.findById(id);
        return category;


    }
    //更新分类
    @Override
    public void update(Category category) {

        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
        System.out.println("更新成功");

    }
    //删除分类
    @Transactional // 开启事务
    @Override
    public void delete(Integer id) {
        categoryMapper.unlinkMusicFromCategory(id);
        categoryMapper.delete(id);
        System.out.println("删除成功");

    }
}
